<script setup lang="ts">
import { SortOptions } from '../type'
import { ISortField, ISortMethod } from '@/service/api/type/goods.type'
import { reactive, Ref, ref, watch } from 'vue'

const emit = defineEmits<{
  (e: 'checkOptions', options: SortOptions): void
}>()

const options: SortOptions = reactive({
  inventory: false,
  onlyDiscount: false,
  sortField: null,
  sortMethod: null
})

const sortMethods: Ref<ISortField> = ref(null)
const priceSortWays: Ref<ISortMethod> = ref('desc')
const changeSort = (sortWays: ISortField) => {
  sortMethods.value = sortWays
  options.sortField = sortWays
  if (sortWays === 'price') {
    if (priceSortWays.value === 'desc') {
      priceSortWays.value = 'asc'
      options.sortMethod = 'desc'
    } else {
      priceSortWays.value = 'desc'
      options.sortMethod = 'asc'
    }
  }
}

watch(
  () => options,
  (newvalue) => {
    console.log(newvalue)
    emit('checkOptions', options)
  },
  {
    deep: true
  }
)
</script>

<template>
  <div class="sub-sort">
    <div class="sort">
      <a
        href="javascript:;"
        :class="{ active: sortMethods === null }"
        @click="changeSort(null)"
        >默认排序</a
      >
      <a
        href="javascript:;"
        :class="{ active: sortMethods === 'publishTime' }"
        @click="changeSort('publishTime')"
        >最新商品</a
      >
      <a
        href="javascript:;"
        :class="{ active: sortMethods === 'orderNum' }"
        @click="changeSort('orderNum')"
        >最高人气</a
      >
      <a
        href="javascript:;"
        :class="{ active: sortMethods === 'evaluateNum' }"
        @click="changeSort('evaluateNum')"
        >评论最多</a
      >
      <a href="javascript:;" @click="changeSort('price')">
        价格排序
        <i class="arrow up" :class="{ active: priceSortWays === 'desc' }" />
        <i class="arrow down" :class="{ active: priceSortWays === 'asc' }" />
      </a>
    </div>
    <div class="check">
      <Checkbox v-model="options.onlyDiscount">仅显示有货商品 </Checkbox>
      <Checkbox v-model="options.inventory">仅显示特惠商品</Checkbox>
    </div>
  </div>
</template>

<style lang="less" scoped>
.sub-sort {
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .sort {
    display: flex;
    a {
      height: 30px;
      line-height: 28px;
      border: 1px solid #e4e4e4;
      padding: 0 20px;
      margin-right: 20px;
      color: #999;
      border-radius: 2px;
      position: relative;
      transition: all 0.3s;
      &.active {
        background: @themeColor;
        border-color: @themeColor;
        color: #fff;
      }
      .arrow {
        position: absolute;
        border: 5px solid transparent;
        right: 8px;
        &.up {
          top: 3px;
          border-bottom-color: #bbb;
          &.active {
            border-bottom-color: @themeColor;
          }
        }
        &.down {
          top: 15px;
          border-top-color: #bbb;
          &.active {
            border-top-color: @themeColor;
          }
        }
      }
    }
  }
  .check {
    .checkbox {
      margin-left: 20px;
      color: #999;
    }
  }
}
</style>
